package com.mud.mudcommune.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.LoginEntry;
import com.mud.mudcommune.http.ApiService;
import com.mud.mudcommune.http.NetUtils;
import com.mud.mudcommune.utils.ACache;
import com.mud.mudcommune.view.fragment.MineFragment;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, Callback<LoginEntry> {
    @BindView(R.id.login_back)
    ImageView loginBack;
    @BindView(R.id.forget_psd)
    TextView forgetPassword;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.login_textview)
    TextView login;
    @BindView(R.id.login_username)
    EditText login_username;
    @BindView(R.id.login_password)
    EditText login_password;
    Unbinder unbinder;
    @BindView(R.id.Login_progressBar)
    ProgressBar progressBar;
    private ACache aCache = null;
    private String CONSTANTS_KEY_STRING = null;//数据保存地址
    private String username = null;
    private String password = null;
    private boolean AcacheFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        unbinder = ButterKnife.bind(this);
        if (aCache == null) {
            aCache = ACache.get(this);
            File sdcardFileDir = Environment.getExternalStorageDirectory();
            File file = new File(sdcardFileDir.getAbsolutePath(), "Mudlogin.txt");
            CONSTANTS_KEY_STRING = file.getAbsolutePath();
        }
        Acache();
    }

    private void Acache() {
        String usernameAcache = aCache.getAsString("username");
        String passwordAcache = aCache.getAsString("password");
        if (usernameAcache != null && passwordAcache != null) {
            login_username.setText(usernameAcache);
            login_password.setText(passwordAcache);
            AcacheFlag = true;
        }
    }

    @OnClick({R.id.login_textview, R.id.register, R.id.login_back, R.id.forget_psd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register:
                Intent registerIntent = new Intent(this, RegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.login_back:
                finish();
                break;
            case R.id.forget_psd:
                Intent intent = new Intent(this, FindPasswordStepOneActivity.class);
                startActivity(intent);
                break;
            case R.id.login_textview:
                if (!AcacheFlag) {
                    username = login_username.getText().toString();
                    password = login_password.getText().toString();
                    if (username.equals("") || username == null || this.password.equals("") || username == null) {
                        Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        progressBar.setVisibility(View.VISIBLE);
                        aCache.put("username", username);
                        aCache.put("password", this.password);//缓存
                        NetUtils.createService(ApiService.class, ApiService.BASE_URL).
                                Login(username, this.password).//电话号码就是账号
                                enqueue(this);
                    }
                } else {
                    username = login_username.getText().toString();
                    MineFragment.setUserName(username);
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // progressBar.setVisibility(View.GONE);
                            finish();
                        }
                    }, 500);
                    //登录时延迟1.5秒 然后登录
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onResponse(Call<LoginEntry> call, Response<LoginEntry> response) {
        LoginEntry body = response.body();
        if (body.getStatus() == 200) {
            progressBar.setVisibility(View.GONE);
            MineFragment.setUserName(username);
            finish();//退出登录界面
        } else {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(this, "用户名不存在或密码不正确", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(Call<LoginEntry> call, Throwable t) {
        Toast.makeText(this, "数据请求失败", Toast.LENGTH_SHORT).show();
    }
}
